class Item:
    def __init__(self,game,iname,ilevel=1):
        self.game = game
        self.iname = iname
        self.ilevel = ilevel
        self.iequipped = False
        (self.idef,self.iprefixes,self.iskill) = read_name(self.game,self.iname)
        self.iratio = get_ratio(self.game,self.iprefixes)
        self.iclass = self.idef['class'].strip()
        self.itype = self.idef['type'].strip()
        self.idescription = self.idef['description']
        if self.iclass == 'weapon' or self.iclass == 'armor':
            self.iequippable = True
        else:
            self.iequippable = False

        self.istats = {}
        for stat in game.stats:
            self.istats[stat] = str(self.iratio) + ' * ( '
            if stat in self.idef:
                self.istats[stat] += ' + ' + self.idef[stat].strip()
            for prefix in self.iprefixes:
                if stat in prefix:
                    self.istats[stat] += ' + ' + prefix[stat].strip()
            self.istats[stat] += ' )'
                    
        self.idmg = {}
        self.iarm = {}
        for typ in game.dmgs:
            dmg = 'dmg_'+typ
            arm = 'arm_'+typ
            self.idmg[typ] = str(self.iratio) + ' * ('
            self.iarm[typ] = str(self.iratio) + ' * ('
            if dmg in self.idef:
                self.idmg[typ] += ' + ' + self.idef[dmg]
            if arm in self.idef:
                self.iarm[typ] += ' + ' + self.idef[arm]
            for prefix in self.iprefixes:
                if dmg in prefix:
                    self.idmg[typ] += ' + ' + prefix[dmg]
                if arm in prefix:
                    self.iarm[typ] += ' + ' + prefix[arm]
            self.idmg[typ] += ' )'
            self.iarm[typ] += ' )'

    def equip(self):
        self.iequipped = True

    def unequip(self):
        self.iequipped = False

    def get_stat(self,stat):
        if stat in self.game.stats:
            return self.istats[stat]
        if stat[:3] == 'dmg':
            return self.idmg[stat[4:]]
        if stat[:3] == 'arm':
            return self.iarm[stat[4:]]
        return 0

def read_name(game,name):
    '''parse item name'''
    tokens = name.split()
    
    start = 0
    prefixes = []
    p = game.get_prefix(tokens[start])
    while p:
        prefixes.append(p)
        start += 1
        p = game.get_prefix(tokens[start])
        
    if 'of' in tokens:
        index = tokens.index('of')
        item = game.get_item(' '.join(tokens[start:index]))
        suffix = game.get_skill(' '.join(tokens[+1:]))
    else:
        item = game.get_item(' '.join(tokens[start:]))
        suffix = None
        
    return (item,prefixes,suffix)

def get_ratio(game,prefixes):
    '''get ratio from prefixes'''
    ratio = 1.0
    for prefix in prefixes:
        if 'ratio' in prefix:
            ratio *= float(prefix['ratio'])
    return ratio

def get_stat(stat,definition,modifier):
    '''get stat from definition list and modifier'''
    ret = 0
    for defi in definition:
        if stat in defi:
            ret += float(defi[stat])
    ret *= modifier
    return ret
